/**Created by the LayaAirIDE*/

module view{
	export class GameStart extends ui.GameStartUI{
		//鱼线级别
    	private lineLevel : number;
		//鱼钩级别
		private hookLevel : number;
		//玩家的金币数量
		private gold : number;

		constructor(){
			super();

			//初始化玩家数据
			this.lineLevel = ComCfg.InitLineLevel;
			this.hookLevel = ComCfg.InitHookLevel;
			this.gold = 0;

			this.btnFish.on(Laya.Event.CLICK, this, this.OnBtnFish);
			this.btnBuyLine.on(Laya.Event.CLICK, this, this.OnBtnBuyLine);
			this.btnbuyHook.on(Laya.Event.CLICK, this, this.OnBtnBuyHook);
		}

		Init() : void {
			this.labLineLen.text = ComCfg.LineLevel[this.lineLevel-1][0].toString();
			this.btnBuyLine.text.text = "￥" + ComCfg.LineLevel[this.lineLevel-1][1].toString();
			this.labHookNum.text = ComCfg.HookLevel[this.hookLevel-1][0].toString();
			this.btnbuyHook.text.text = "￥" + ComCfg.HookLevel[this.hookLevel-1][1].toString();
			this.labGold.text = "￥" + this.gold.toString();

			this.CheckGold();
		}

		//检测金币数量，并设置按钮状态
		CheckGold() : void {
			if(this.gold >= ComCfg.LineLevel[this.lineLevel-1][1]){
				this.btnBuyLine.disabled = false;
			}
			else{
				this.btnBuyLine.disabled = true;
			}

			if(this.gold >= ComCfg.HookLevel[this.hookLevel-1][1]){
				this.btnbuyHook.disabled = false;
			}
			else{
				this.btnbuyHook.disabled = true;
			}
		}

		//开始一次钓鱼
		OnBtnFish() : void {
			if (!GameMain.viewGame) {
				GameMain.viewGame = new view.Game();
			}

			//移除开始页面
			this.removeSelf();
			//显示游戏页面
			GameMain.viewGame.Start(ComCfg.LineLevel[this.lineLevel-1][0], ComCfg.HookLevel[this.hookLevel-1][0]);
			Laya.stage.addChild(GameMain.viewGame);
		}

		//购买鱼线
		OnBtnBuyLine() : void {
			if(this.lineLevel >= ComCfg.LineLevel.length){
				return;
			}
			if(this.gold >= ComCfg.LineLevel[this.lineLevel-1][1]){
				this.gold -= ComCfg.LineLevel[this.lineLevel-1][1];
				this.lineLevel += 1;
				this.CheckGold();
				this.labLineLen.text = ComCfg.LineLevel[this.lineLevel-1][0].toString();
				this.btnBuyLine.text.text = "￥" + ComCfg.LineLevel[this.lineLevel-1][1].toString();
				this.labGold.text = "￥" + this.gold.toString();
			}
		}

		//购买鱼钩
		OnBtnBuyHook() : void {
			if(this.hookLevel >= ComCfg.HookLevel.length){
				return;
			}
			if(this.gold >= ComCfg.HookLevel[this.hookLevel-1][1]){
				this.gold -= ComCfg.HookLevel[this.hookLevel-1][1];
				this.hookLevel += 1;
				this.CheckGold();
				this.labHookNum.text = ComCfg.HookLevel[this.hookLevel-1][0].toString();
				this.btnbuyHook.text.text = "￥" + ComCfg.HookLevel[this.hookLevel-1][1].toString();
				this.labGold.text = "￥" + this.gold.toString();
			}
		}

		//完成一次钓鱼后的处理
		FinishOnceGame(getFishs:number[]) : void {
			//计算本次金币
			let getGold : number = 0;
			getFishs.forEach(i => {
				getGold += ComCfg.Scores[i-1];
			});
			this.gold += getGold;
			this.labGold.text = this.gold.toString();
			this.CheckGold();
		}
	}
}